/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.rmgmt.RemoteManager;
/*     */ import com.zimbra.cs.rmgmt.RemoteResult;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.net.Inet4Address;
/*     */ import java.net.Inet6Address;
/*     */ import java.net.InetAddress;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetServerNIFs
/*     */   extends AdminDocumentHandler
/*     */ {
/*  50 */   private static final Pattern ADDR_PATTERN = Pattern.compile("(addr):([0-9a-z\\.:]+)", 2);
/*  51 */   private static final Pattern MASK_PATTERN = Pattern.compile("(mask):([0-9\\.xabcdef]+)", 2);
/*     */   private static final int KEY_GROUP = 1;
/*     */   private static final int VALUE_GROUP = 2;
/*     */   private static final String IPV4 = "ipv4";
/*     */   private static final String IPV6 = "ipv6";
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  59 */     ZimbraSoapContext lc = getZimbraSoapContext(context);
/*     */     
/*  61 */     String ipAddressType = request.getAttribute("type", null);
/*  62 */     boolean ipV4 = false;boolean ipV6 = false;
/*  63 */     if (StringUtil.equalIgnoreCase(ipAddressType, "ipv6")) {
/*  64 */       ipV6 = true;
/*  65 */     } else if (StringUtil.equalIgnoreCase(ipAddressType, "both")) {
/*  66 */       ipV4 = true;
/*  67 */       ipV6 = true;
/*     */     }
/*     */     else {
/*  70 */       ipV4 = true;
/*     */     }
/*  72 */     Element serverEl = request.getElement("server");
/*  73 */     String method = serverEl.getAttribute("by");
/*  74 */     String serverName = serverEl.getText();
/*  75 */     Provisioning prov = Provisioning.getInstance();
/*  76 */     Server server = prov.get(Key.ServerBy.fromString(method), serverName);
/*  77 */     if (server == null) {
/*  78 */       throw ServiceException.INVALID_REQUEST("Cannot find server record for the host: " + serverName, null);
/*     */     }
/*     */     
/*  81 */     RemoteManager rmgr = RemoteManager.getRemoteManager(server);
/*  82 */     RemoteResult rr = rmgr.execute("zmserverips");
/*  83 */     BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(rr.getMStdout())));
/*     */     
/*     */ 
/*  86 */     Element response = lc.createElement(AdminConstants.GET_SERVER_NIFS_RESPONSE);
/*     */     try { String line;
/*  88 */       while ((line = in.readLine()) != null) {
/*  89 */         Matcher IPmatcher = ADDR_PATTERN.matcher(line);
/*  90 */         Matcher maskMatcher = MASK_PATTERN.matcher(line);
/*  91 */         if ((IPmatcher.find()) && (maskMatcher.find())) {
/*  92 */           String ipAddress = IPmatcher.group(2).toLowerCase();
/*  93 */           InetAddress addressType = InetAddress.getByName(ipAddress);
/*  94 */           if (((!(addressType instanceof Inet6Address)) || (ipV6)) && (
/*     */           
/*  96 */             (!(addressType instanceof Inet4Address)) || (ipV4)))
/*     */           {
/*     */ 
/*  99 */             String type = (addressType instanceof Inet4Address) ? "ipv4" : "ipv6";
/* 100 */             Element elNIF = response.addElement("ni");
/* 101 */             elNIF.addElement("a").addAttribute("n", IPmatcher.group(1).toLowerCase()).addAttribute("type", type).setText(ipAddress);
/* 102 */             elNIF.addElement("a").addAttribute("n", maskMatcher.group(1).toLowerCase()).setText(maskMatcher.group(2));
/*     */           }
/*     */         }
/*     */       }
/* 106 */     } catch (IOException e) { throw ServiceException.FAILURE("exception occurred handling CLI command", e);
/*     */     }
/* 108 */     return response;
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 113 */     notes.add("Do not need any right, all admins are allowed.");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/GetServerNIFs.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */